﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Agen.Models;
using System.ComponentModel;
using System.Diagnostics;

namespace Agen.Views
{
    public partial class LoginPage : Page
    {
       private LoginVO vo;

        public LoginPage()
        {
            InitializeComponent();
            vo = new LoginVO();
            DataContext = vo;
            ApplicationManager.client.checkLoginCompleted += new EventHandler<ServiceAgencyWPF.checkLoginCompletedEventArgs>(client_checkLoginCompleted);
        }

        void client_checkLoginCompleted(object sender, ServiceAgencyWPF.checkLoginCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                Debug.WriteLine("DBG : LOGIN_PAGE => check_login ");
                ApplicationManager.CURRENT_EMPLOYEE = e.Result;
                ApplicationManager.FrameAction(this, ApplicationManager.USER_LIST_HANDLER, new Object());
            }
            else {
                MessageBox.Show("Bad Connection Information");
            }


        }

        private void login_btn_Click(object sender, RoutedEventArgs e)
        {
            if (vo.Login != "" && vo.Password != "" )
            {
                ApplicationManager.client.checkLoginAsync(vo.Login, vo.Password);
            }
            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ApplicationManager.CURRENT_EMPLOYEE = null;
         }

        

    }

    public class LoginVO : INotifyPropertyChanged
    {

        private string _login;
        private string _password;


        public string Login {
            get
            {
                return this._login;
            }
            set
            {
                this._login = value;
                OnPropertyChanged("Login");
            }
        
        }


        public string Password
        {
            get
            {
                return this._password;
            }
            set
            {
                this._password = value;
                OnPropertyChanged("Password");
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

    }
}